package com.imooc.controller;

import com.imooc.enums.YesOrNo;
import com.imooc.pojo.Carousel;
import com.imooc.pojo.Category;
import com.imooc.service.CarouselService;
import com.imooc.service.CategoryService;
import com.imooc.utils.IMOOCJSONResult;
import com.imooc.vo.NewItemVo;
import com.imooc.vo.RootCategoryVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author wangke
 * @version 1.0.0
 * @Description 轮播图控制器
 * @createTime 2021年07月11日 22:20:00
 */
@RestController
@RequestMapping("/index")
@Api(value = "首页",tags = "首页数据相关接口")
public class IndexController {
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/carousel")
    @ApiOperation(value = "轮播图查询",notes = "轮播图列表查询",httpMethod = "GET")
    public IMOOCJSONResult queryCarouselList(){
        List<Carousel> carousels = carouselService.queryAll(YesOrNo.yes.type);
        return IMOOCJSONResult.ok(carousels);

    }
    @GetMapping("/cats")
    @ApiOperation(value = "一级菜单接口" )
    public IMOOCJSONResult getCategoryParentLevel(){

        List<Category> categories = categoryService.getCategoryParentLevel();

        return IMOOCJSONResult.ok(categories);
    }
    @GetMapping("/subCat/{catId}")
    public IMOOCJSONResult querySubCategory(@PathVariable Integer catId){
        if(catId==null){
            return IMOOCJSONResult.errorMsg("分类信息错误");
        }

        List<RootCategoryVo> rootCategoryVos = categoryService.queryRootCategory(catId);
        return IMOOCJSONResult.ok(rootCategoryVos);
    }
    @GetMapping("/sixNewItems/{rootCatId}")
    public IMOOCJSONResult getItemLazy( @PathVariable Integer rootCatId){
        if(rootCatId==null){
            IMOOCJSONResult.errorMsg("分类信息错误");
        }
        List<NewItemVo> itemsLazy = categoryService.getItemsLazy(rootCatId);
        return IMOOCJSONResult.ok(itemsLazy);
    }
}
